package dao;

import java.io.IOException;
import java.io.Reader;
import java.sql.SQLException;
import java.util.List;

import com.ibatis.common.resources.Resources;
import com.ibatis.sqlmap.client.SqlMapClient;
import com.ibatis.sqlmap.client.SqlMapClientBuilder;



import domain.TPrivilege;


public class MenuDaoImpl implements MenuDao {
	private static SqlMapClient sqlMap;
	void init() {
	    try {
	      Reader reader = Resources.getResourceAsReader("SqlMapConfig.xml");
	      sqlMap = SqlMapClientBuilder.buildSqlMapClient(reader);
	      reader.close(); 
	    } catch (IOException e) {
	      // Fail fast.
	      throw new RuntimeException("Something bad happened while building the SqlMapClient instance." + e, e);
	    }
	  }



	public TPrivilege queryMenuRoot() 
	{
		this.init();
		TPrivilege root = null;
		 try {
			 root= (TPrivilege)sqlMap.queryForObject("selectRoot",-1);
		} catch (SQLException e) {
			e.printStackTrace();
		} 
		return root;
	}
	
	@SuppressWarnings("unchecked")
	public List<TPrivilege> queryMenuNode(int parentId)
	{
		this.init();
		List<TPrivilege> list = null;
		 try {
			 list= sqlMap.queryForList("selectByParentId",-1);
		} catch (SQLException e) {
			e.printStackTrace();
		} 
		return list;
	}
	
	@SuppressWarnings("unchecked")
	public List<TPrivilege>  getNextLevel(int nodeId)
	{
		this.init();
		List<TPrivilege> list = null;
		 try {
			 list= sqlMap.queryForList("selectByParentId",nodeId);
		} catch (SQLException e) {
			e.printStackTrace();
		} 
		return list;
	}

}
